package JAVA300.第九章容器;

import java.util.*;

/***
 * 测试迭代器遍历list map Set
 */

public class TestIterator {
    public static void main(String[] args) {
       // testIteratorList();
       // testIteratorSet();
        testIteratorMap();
    }

    public static void testIteratorList(){

        List<String> list = new ArrayList<>();
        list.add("aa");
        list.add("bb");
        list.add("cc");

        for(Iterator<String> iterator = list.iterator();iterator.hasNext();){
            String temp = iterator.next();//指针下移，返回该指针所指向的元素
            System.out.println(temp);
        }

    }

    public static void testIteratorSet(){

        Set<String> set = new HashSet<>();
        set.add("aa");
        set.add("bb");
        set.add("cc");

        for(Iterator<String> iterator = set.iterator();iterator.hasNext();){
            String temp = iterator.next();//指针下移，返回该指针所指向的元素
            System.out.println(temp);
        }

    }

    public static void testIteratorMap(){

        Map<Integer,String> map = new HashMap<>();
        map.put(1,"aa");
        map.put(2,"bb");
        map.put(3,"cc");

        //方式1
//        Set<Map.Entry<Integer,String>>  map_set = map.entrySet();
//
//        for(Iterator<Map.Entry<Integer,String>> iterator = map_set.iterator(); iterator.hasNext();){
//            Map.Entry<Integer,String> temp = iterator.next();//指针下移，返回该指针所指向的元素
//            System.out.println(temp.getKey()+"----"+temp.getValue());
//        }

        //方式2
        Set<Integer> map_key = map.keySet();
        for (Iterator<Integer> iterator = map_key.iterator();iterator.hasNext();){
            Integer key = iterator.next();
            System.out.println(key+"---"+map.get(key));
        }
    }

}
